import datetime
from collections import defaultdict


class Bill:
    def __init__(self, date, amount, category, remark):
        self.date = date
        self.amount = amount
        self.category = category
        self.remark = remark


class BillManager:
    def __init__(self):
        self.income_bills = []
        self.expense_bills = []

    def add_income(self, date, amount, category, remark):
        if amount <= 0:
            print("错误：收入金额必须为正数。")
            return
        self.income_bills.append(Bill(date, amount, category, remark))

    def add_expense(self, date, amount, category, remark):
        if amount <= 0:
            print("错误：支出金额必须为正数。")
            return
        self.expense_bills.append(Bill(date, amount, category, remark))

    def display_bills(self):
        print("收入账单:")
        for bill in self.income_bills:
            print(f"日期: {bill.date}, 金额: {bill.amount}, 类别: {bill.category}, 备注: {bill.remark}")

        print("\n支出账单:")
        for bill in self.expense_bills:
            print(f"日期: {bill.date}, 金额: {bill.amount}, 类别: {bill.category}, 备注: {bill.remark}")

    def query_bills(self, date_or_range, category=None):
        def parse_date(date_str):
            try:
                return datetime.datetime.strptime(date_str, "%Y-%m-%d").date()
            except ValueError:
                print("错误：无效的日期格式，请使用YYYY-MM-DD格式。")
                return None

        start_date, end_date = None, None
        if "-" in date_or_range:
            start_str, end_str = date_or_range.split("-")
            start_date = parse_date(start_str)
            end_date = parse_date(end_str)
            if not start_date or not end_date:
                return
        else:
            target_date = parse_date(date_or_range)
            if not target_date:
                return
            start_date, end_date = target_date, target_date

        def match_bill(bill):
            if category and bill.category != category:
                return False
            if start_date and bill.date < start_date:
                return False
            if end_date and bill.date > end_date:
                return False
            return True

        print("查询结果 - 收入账单:")
        for bill in self.income_bills:
            if match_bill(bill):
                print(f"日期: {bill.date}, 金额: {bill.amount}, 类别: {bill.category}, 备注: {bill.remark}")

        print("\n查询结果 - 支出账单:")
        for bill in self.expense_bills:
            if match_bill(bill):
                print(f"日期: {bill.date}, 金额: {bill.amount}, 类别: {bill.category}, 备注: {bill.remark}")


def main():
    bill_manager = BillManager()

    while True:
        print("\n个人账单管理系统")
        print("1. 记录收入")
        print("2. 记录支出")
        print("3. 账单展示")
        print("4. 账单查询")
        print("5. 退出")
        choice = input("请选择操作: ")

        if choice == '1':
            date = input("请输入日期 (YYYY-MM-DD): ")
            amount = float(input("请输入金额: "))
            category = input("请输入类别: ")
            remark = input("请输入备注 (可选): ")
            bill_manager.add_income(date, amount, category, remark)
        elif choice == '2':
            date = input("请输入日期 (YYYY-MM-DD): ")
            amount = float(input("请输入金额: "))
            category = input("请输入类别: ")
            remark = input("请输入备注 (可选): ")
            bill_manager.add_expense(date, amount, category, remark)
        elif choice == '3':
            bill_manager.display_bills()
        elif choice == '4':
            date_or_range = input("请输入日期或日期范围 (YYYY-MM-DD 或 YYYY-MM-DD-YYYY-MM-DD): ")
            category = input("请输入类别 (可选): ")
            bill_manager.query_bills(date_or_range, category)
        elif choice == '5':
            print("退出系统。")
            break
        else:
            print("错误：无效的选择，请重新选择。test---->feature")


if __name__ == "__main__":
    main()